<template>
  <d2-container>
    <el-main>

      <el-form ref="staffForm" :model="messageForm" label-width="120px" label-position="left"
               :rules="formRules">

        <el-form-item label="消息" prop="message">
          <el-input placeholder="请输入消息" v-model="messageForm.message" :disabled="disabled"></el-input>
        </el-form-item>

        <el-footer style="text-align: center">
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="resetForm('staffForm')">重置</el-button>
          <el-button @click="edit" v-show="editShow">修改</el-button>
        </el-footer>

      </el-form>

    </el-main>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import {mapState} from "vuex"
export default {
  data() {
    return {
      dataSearch: {},
      dataList: [],
      messageForm: {
        message: undefined
      },
      editShow: false,
      disabled: false,
      id: undefined,
      startDate: undefined,

      formRules: {
        message: [
          {required: true, message: '请输入消息', trigger: 'blur'},
        ]
      },
    };
  },
  computed:{
    ...mapState('d2admin/user',['info'])
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    edit() {
      this.disabled = false;
    },

    getSchoolList() {
      // this.schoolList = this.info.school;
      // this.staffForm.school = this.schoolList[0]._id;
      let data = {};
          data.page = 1;
          data.pageSize = 9999999;
          if (this.schoolname != ''){
            data.name = this.schoolname
          }
          request({
            url: "/school/getSchoolList",
            method: "post",
            data
          }).then(res => {
            this.schoolList = res.data.school;
            this.schoolname = ''
            console.log(res.data.school,'dsds')
          });

      // this.getClassList(1, 9999, this.staffForm.school);

      // this.info.role.forEach(value=>{
      //   if (value.name==='root') {
      //     let data = {};
      //     data.page = 1;
      //     data.pageSize = 9999999;
      //     request({
      //       url: "/school/getSchoolList",
      //       method: "post",
      //       data
      //     }).then(res => {
      //       this.schoolList = res.data.school;
      //     });
      //   }
      // })
    },

    addSchool(e){
      if (this.staffForm.school.map(function(e){return e._id}).indexOf(e._id) == -1){
        this.staffForm.school.push(e)
      }

    },

    getStudentList(){
      const id = this.$route.query.id;
      const data = {id}
      request({
        url: "/staff/getStudentList",
        method: "post",
        data
      }).then(res => {
        this.dataList = res.data;
        this.dataList.forEach((value, index) => {
          this.dataList[index].gender = value.gender === 0 ? '男' : '女'
        })
        this.staffForm.studentNum = this.dataList.length || 0
      });
    },

    formSubmit(){
      this.getStudentList()
    },

    removeSchool(index){
      if (index != 0){
        this.staffForm.school.splice(index,1)
      }
      else{
        this.$message('不能删除该员工主校区')
      }
    },
    getDepartmentList(page, pageSize, school) {
      // this.staffForm.department = undefined
      let data = {};
      data.page = page;
      data.pageSize = pageSize;
      data.school = school;
      request({
        url: "/department/getDepartmentList",
        method: "post",
        data
      }).then(res => {
          this.departmentList = res.data.department;
          let index = this.departmentList.map(function(e){return e._id}).indexOf(this.staffForm.department)
          if (index == -1){
            this.staffForm.department = undefined
          }
      });
    },
    getPositionList(page) {
      request({
        url: "/position/getPositionList",
        method: "post",
        data : {school:this.staffSchool}
      }).then(res => {
        this.positionList = res.data.position;
        // 返回数据
      });
    },
    getReportTargetList(page, pageSize) {
      let data = {};
      (data.page = page), (data.pageSize = pageSize);
      // data.department = this.staffForm.department
      data.school = this.school
      request({
        url: "/staff/getStaffList",
        method: "post",
        data
      }).then(res => {
        this.reportTargetList = res.data.staff;
        // 返回数据
      });
    },

    searchSchool(){
      this.getSchoolList()
    },
    getClassList(page, pageSize, school) {
      let data = {};
      data.page = page;
      data.pageSize = pageSize;
      data.school = school;
      request({
        url: "/class/getClassList",
        method: "post",
        data
      }).then(res => {
        this.classList = []
        for (let i = 0; i < res.data.class.length; i++) {
          let key = res.data.class[i]._id;
          let label = res.data.class[i].name;
          this.classList.push({key: key, label: label})
        }
        if (this.$route.query.id) {
          this.getStaffOne(this.$route.query.id);
        }

      });
    },
    schoolSelected() {
      this.getDepartmentList(1, 9999, this.staffForm.school);
    },
    departmentSelected() {
      // this.getClassList(1, 99999, this.staffForm.school);
      this.getReportTargetList(1, 99999);
    },
    submit() {
      let data = {};
      let url = "/message/addMessageModel";
      if (this.id != undefined) {
        data.staff = this.id;
        url = "/message/updateMessageModel";
      }
      data.message = this.messageForm.message;

      // data.school = this.staffForm.school
      request({
        url: url,
        method: "post",
        data
      }).then(res => {
        if (res.code == 200) {
          this.refresh = true
          this.messageForm = {
            message:undefined
      }
          this.$router.push({path: "/message/messageModelList"})
          this.$alert("", "提交成功", {
            confirmButtonText: "确定"
          });
        }
        // 返回数据
      });
    },
    getStaffOne(id) {
      request({
        url: "/staff/getStaffOne",
        method: "post",
        data: {id: id}
      }).then(res => {
        if (res.code == 200) {
          this.id = res.data._id;
          this.staffForm.name = res.data.name;
          this.staffForm.gender = res.data.gender;
          this.staffForm.cellphone = res.data.cellphone;
          this.staffForm.homeAddress = res.data.homeAddress;
          this.staffForm.entryDate = res.data.entryDate;
          this.staffForm.position = res.data.position != undefined ? res.data.position._id : undefined;
          this.staffForm.reportTarget = res.data.reportTarget.map(value => value._id);
          this.reportTargetList = res.data.reportTarget;
          this.staffForm.department = res.data.department;
          this.staffForm.school = res.data.school;
          this.staffForm.birthday = res.data.birthday;
          this.staffForm.continuousProportion = res.data.continuousProportion;
          this.staffForm.changeProportion = res.data.changeProportion;
          this.staffForm.inviteProportion = res.data.inviteNum;
          this.staffForm.attendence = res.data.attendence;
          this.staffForm.classSelected = res.data.class.map(value => value._id);
          this.staffForm.basicSalary = res.data.basicSalary || 0;
          this.staffForm.fixedPosition = res.data.position != undefined ? res.data.position._id : undefined;
          this.staffForm.staffBonus = res.data.staffBonus != undefined ? res.data.staffBonus : undefined
          // this.classBonus = res.data.classBonus == undefined ? 0 :res.data.classBonus
          this.getDepartmentList(1, 9999, res.data.school[0]._id);
          this.staffSchool = res.data.school[0]._id
          this.getPositionList(1, 99999);

        }
      });
    },


        beforeRouteLeave(to,from,next){

        next()
        },
        },

  created() {
  }
};
</script>
